package com.example.hzq.food.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.hzq.food.R;
import com.example.hzq.food.activity.Orderdetail;
import com.example.hzq.food.bean.FoodBean;
import com.example.hzq.food.bean.OrderBean;
import com.example.hzq.food.model.Model;
import com.squareup.picasso.Picasso;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by hzq on 2017/6/28.
 */

public class Orderadapter extends RecyclerView.Adapter{
    private String foodimage;
    private Context context;
    private LayoutInflater layoutInflater;//动态加载布局
    private List<OrderBean> list;//保存要显示的数据
    public Orderadapter(Context context) {
        this.context=context;
        layoutInflater=LayoutInflater.from(context);
    }
    public void setList(List<OrderBean> list){
        this.list=list;
        notifyDataSetChanged();
    }
    public class ViewHolder extends RecyclerView.ViewHolder{
        private ImageView foodimage;
        private TextView shopname,ordertime,foodname,price,num,sum;
        public ViewHolder(View itemView) {
            super(itemView);
            foodimage = (ImageView) itemView.findViewById(R.id.foodimage);
            shopname = (TextView) itemView.findViewById(R.id.shopname);
            ordertime = (TextView) itemView.findViewById(R.id.ordertime);
            foodname = (TextView) itemView.findViewById(R.id.orderfoodname);
            price = (TextView) itemView.findViewById(R.id.foodprice);
            num = (TextView) itemView.findViewById(R.id.orderfoodnum);
            sum = (TextView) itemView.findViewById(R.id.orderfoodsum);
        }
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = layoutInflater.inflate(R.layout.item_order, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final OrderBean order = list.get(position);
        if (order == null)
            return;
        final ViewHolder viewholder = (ViewHolder) holder;
        Model model=new Model();
        Call<FoodBean> call= model.getfoodbyid(order.getFood_id());
        call.enqueue(new Callback<FoodBean>() {
            @Override
            public void onResponse(Call<FoodBean> call, Response<FoodBean> response) {
                if(response.isSuccessful()&&response.body()!=null)
                {
                    if(!response.body().getPic().equals(""))
                        foodimage=response.body().getPic();
                        Picasso.with(context).load(response.body().getPic()).placeholder(R.drawable.ic_default_adimage).into(viewholder.foodimage);
                }
                else
                {
                    Toast.makeText(context,"获取资源失败",Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<FoodBean> call, Throwable t) {
                Toast.makeText(context,"请检查网络连接",Toast.LENGTH_SHORT).show();
            }
        });
        viewholder.shopname.setText(order.getShopname());
        viewholder.ordertime.setText(order.getOrdertime().substring(0,19)+"");
        viewholder.foodname.setText("菜品:"+order.getFoodname()+"");
        viewholder.price.setText("单价:"+order.getPrice()+"");
        viewholder.num.setText("数量:"+order.getNum()+"");
        viewholder.sum.setText("总价:"+order.getSum()+"");
        viewholder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(context, Orderdetail.class);
                intent.putExtra("imagepath",foodimage);
               intent.putExtra("index",position);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        if(list==null)
            return 0;
        return list.size();
    }
}
